<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<meta name="viewport" content="width=1000, initial-scale=1.0, maximum-scale=1.0">
	    <!-- Loading Bootstrap -->
	    <link href="./Public/admin/Flat/dist/css/vendor/bootstrap.min.css" rel="stylesheet">
	    <!-- Loading Flat UI -->
	    <link href="./Public/admin/Flat/dist/css/flat-ui.css" rel="stylesheet">
	    <link rel="shortcut icon" href="./Public/admin/Flat/img/favicon.ico">
	    <!-- 需要载入的文件 -->
   		<script type="text/javascript" src="./Public/uploadify/jquery-1.8.2.min.js"></script>
   		<script type="text/javascript" src="./Public/uploadify/jquery.uploadify.min.js"></script>
   		<link rel="stylesheet" type="text/css" href="./Public/uploadify/uploadify.css">
   		<!--载入百度编辑器-->
	    	<script type="text/javascript" charset="utf-8" src="Public/admin/ueditor1_4_3/ueditor.config.js"></script>
    		<script type="text/javascript" charset="utf-8" src="Public/admin/ueditor1_4_3/ueditor.all.min.js"> </script>
    		<script type="text/javascript" charset="utf-8" src="Public/admin/ueditor1_4_3/lang/zh-cn/zh-cn.js"></script>
	    <style type="text/css">
	    	.form-control{
	    		margin-bottom: 10px;
	    	}
	    	.alert{
	    		width: 95%;
	    		margin: 0 auto;
	    	}
	    	#from{
	    		width: 95%;
	    		margin: 0 auto;
	    	}
	    	#exampleInputEmail1{
	    		width: 40%;
	    	}
	    	td ul li{
	    		list-style: none;
	    		float: left;
	    		margin: 5px;
	    	}
	    </style>
	    
	</head>
	<body>
		<div class="alert alert-success">添加商品</div>
		<form action="" id="from" method="post" enctype="multipart/form-data">
			<!--基本信息-->
			<label for="exampleInputEmail1">基本信息</label>
			<table class="table table-bordered">
			  <tbody>
			  	<tr>
			  		<td width="250">所属分类</td>
			  		<td>
			  			<select name="cid" class="form-control cate" id="exampleInputEmail1">
			  				<option value="">请选择分类</option>
			  				{{foreach from="$cates" value="$v"}}
			  					<option  <?php if($oldData['cid'] == $v['cid']): ?>selected<?php endif ?> value="{{$v['cid']}}" tid={{$v['type_tid']}}>{{$v['_name']}}</option>
			  				{{endforeach}}
			  			</select>
			  		</td>
			  	</tr>
			  	<tr>
			  		<td>所属品牌</td>
			  		<td>
			  			<select name="bid" class="form-control" id="exampleInputEmail1">
			  				<option value="">所属品牌</option>
			  				{{foreach from="$brands" value="$v"}}
			  					<option  <?php if($oldData['bid'] == $v['bid']): ?>selected<?php endif ?> value="{{$v['bid']}}">{{$v['bname']}}</option>
			  				{{endforeach}}
			  			</select>
			  		</td>
			  	</tr>
			  	<tr>
			  		<td>商品名称</td>
			  		<td><input type="text" name="gname" class="form-control" id="exampleInputEmail1" value="{{$oldData['gname'] }}" /></td>
			  	</tr>
			  	
			  	<tr>
			  		<td>单位</td>
			  		<td><input type="text" name="unit" class="form-control" id="exampleInputEmail1" value="{{$oldData['unit'] }}" /></td>
			  	</tr>
			  	<tr>
			  		<td>市场价</td>
			  		<td><input type="text" name="marketprice" class="form-control" id="exampleInputEmail1" value="{{$oldData['marketprice'] }}" /></td>
			  	</tr>
			  	<tr>
			  		<td>商城价</td>
			  		<td><input type="text" name="shopprice" class="form-control" id="exampleInputEmail1" value="{{$oldData['shopprice'] }}" /></td>
			  	</tr>
			  </tbody>
			</table>
			<!--商品属性-->
			<label for="exampleInputEmail1">商品属性</label>
			<table class="table table-bordered" id="sx">
			  <tbody id="attr">
			  	{{foreach from="$data" value="$v"}}
					{{if value="$v['class'] eq 0"}}
						<tr class="info">
							<td>{{$v['taname']}}</td>
							<td>
								<select name="attr[{{$v['taid']}}]" id="">
									<option value="">请选择</option>
									{{foreach from="$v['tavalue']" value="$value"}}
										<option value="{{$value}}" {{if value="$v['gtvalue'] eq $value"}}selected{{endif}} >{{$value}}</option>
									{{endforeach}}
								</select>
							</td>
						</tr>
					{{endif}}
				{{endforeach}}
			  </tbody>
			</table>
			<!--商品规格-->
			<label for="exampleInputEmail1">商品规格</label>
			<table class="table table-bordered" id="gg">
			  <tbody id="spec">
			  	<?php
					$taid = [];
				?>
				{{foreach from="$data" value="$v"}}
					{{if value="$v['class'] eq 1"}}
						<?php
							$taid[] = $v['taid'];
						?>
						<tr class="info">
							<td>{{$v['taname']}}</td>
							<td>
								<select name="spec[{{$v['taid']}}]" id="">
									<option value="">请选择</option>
									{{foreach from="$v['tavalue']" value="$value"}}
										<option value="{{$value}}" {{if value="$v['gtvalue'] eq $value"}}selected{{endif}} >{{$value}}</option>
									{{endforeach}}
								</select>
							</td>
							<td>
								附加价格: <input type="text" name="spec[{{$v['taid']}}][]" id="">
							</td>
							<td>
								<a href="javascript:;" class="btn btn-info removeSpec goodsAttr{{$v['taid']}}">删除规格</a>
							</td>
						</tr>
					{{endif}}
				{{endforeach}}
			  </tbody>
			  
			</table>
			<?php
			//去除重复的taid
			$taid = array_unique($taid);
			?>
			<script>
				var taid = '<?php echo json_encode($taid) ?>';
				//把js字符串转为json对象
				taid = $.parseJSON(taid);
				//循环json
				$.each(taid,function (k,v) {
					//$('.goodsAttr12').first()...
					$('.goodsAttr' + v).first().removeClass('removeSpec btn-info').addClass('addSpec btn-primary').html('增加规格');
				})
			</script>
			<!--列表图-->
			<label for="exampleInputEmail1">列表图</label>
			<table class="table table-bordered">
			  <tbody>
			  	<td width="250">
			  		上传图片
			  	</td>
			  	<td>
			  		<!--上传插件-->
			  		<div lab="uploadFile">
						<!-- file表单 -->
					    <input id="f" type="file">
					    <span>类型: *.jpg,*.png 大小: 2000KB 数量: 1</span>
					    <div id="uploadList">
					        <ul>
					        	<li>
					        		<img height="80" src="{{$oldData['pic']}}">
					        		<input type="hidden" name="pic" value="{{$oldData['pic']}}">
					        		<a href="javascript:;" id="self-closes">X</a>
					        	</li>
					        </ul>
					    </div>
					</div>
			  	</td>
			  </tbody>
			</table>
			<!--商品图册-->
			<label for="exampleInputEmail1">商品图册</label>
			<table class="table table-bordered">
			  <tbody>
			  	<td width="250">
			  		上传图片
			  	</td>
			  	<td>
			  		<!--上传插件-->
			  		<div lab="uploadFile">
						<!-- file表单 -->
					    <input id="d" type="file" multiple="true">
					    <span>类型: *.jpg,*.png 大小: 2000KB 数量: 4</span>
					    <div id="uploadLists">
					        <ul>
					        	{{foreach from="$oldData['small']" value='$src'}}
					        	<li>
					        		<img height="80" src="{{$src}}">
					        		<input type="hidden" name="detail_img[]" value="{{$src}}">
					        		<a href="javascript:;" id="self-closes">X</a>
					        	</li>
					        	{{endforeach}}
					        </ul>
					    </div>
					</div>
			  	</td>
			  </tbody>
			</table>
			<!--商品详情-->
			<label for="exampleInputEmail1">商品详情</label>
			<table class="table table-bordered">
			  <tbody>
			  	<!--调用百度编辑器-->
				<script id="editor" type="text/plain" style="width:100%;height:500px;" name="intro">{{$oldData['intro'] }}</script>
		   		<script>
		        		var ue = UE.getEditor('editor');
		   		</script>
			  </tbody>
			</table>
			<!--销后服务-->
			<label for="exampleInputEmail1">销后服务</label>
			<table class="table table-bordered">
			  <tbody>
			  	<!--调用百度编辑器-->
				<script id="editor2" type="text/plain" style="width:100%;height:500px;" name="service">{{$oldData['service'] }}</script>
		   		<script>
		        		var ue2 = UE.getEditor('editor2');
		   		</script>
			  </tbody>
			</table>
			<input type="hidden" name="type_tid" id="type_tid" value="{{$oldData['type_tid']}}" />
			<input type="hidden" name="gid" id="type_tid" value="{{Q('get.gid')}}" />
			<button class="btn btn-primary btn-block" type="submit"> 确定 </button>
		</form>
		 <script type="text/javascript">
	        $(function() {
	        	
	        	$('select[name=cid]').change(function(){
				//获得选中的option的tid
				var tid = $('option:selected').attr('tid');
				$("#type_tid").val(tid);
//				console.log(tid);
				$.ajax({
					type:"post",
					url:"{{U('Goods/getAttr')}}",
					data:{t : tid},
					//确定返回数据类型
					dataType:'json',
					success:function(phpData){
						console.log(phpData);
						var attr = '';
						var spec = '';
						$.each(phpData, function(k,v) {    
							//如果是属性
							if(v.class == 0){
								attr += '<tr class="info">';
								attr += '<td>'+v.taname+'</td>';
								attr += '<td>';
								attr += '<select name="attr['+v.taid+']" id="">';
								attr += '<option value="">请选择</option>';
									 $.each(v.tavalue, function(kk,vv) {    
										attr += '<option value="'+vv+'">'+vv+'</option>';
									 });
								attr += '</select>';
								attr += '</td></tr>';
								
							}else{//如果是规格
								spec += '<tr class="info">';
								spec += '<td>'+v.taname+'</td>';
								spec += '<td>';
								spec += '<select name="spec[value]['+v.taid+'][]" id="">';
								spec += '<option value="">请选择</option>';
									$.each(v.tavalue, function(kk,vv) {    
										spec += '<option value="'+vv+'">'+vv+'</option>';                                                          
									});
								spec += '</select></td>';
								spec += '<td>附加价格：<input type="text" name="spec[added]['+v.taid+'][]" id=""/></td>';
								spec += '<td>';
								spec += '<a href="javascript:;" class="btn btn-primary addSpec">添加规格</a>';
								spec += '</td></tr>';
							}
							                                                        
						});
						
						$('#attr').html(attr);
						$('#spec').html(spec);
					}
				});
			});
	        	
	        //点击添加规格
			$('.addSpec').live('click',function(){
				//找到当前的父级的tr
				var trObj = $(this).parents('tr');
				//克隆一个
				var tr = trObj.clone();
				tr.find('.addSpec').html('删除规格').removeClass('btn-primary addSpec').addClass('btn-info removeSpec');
				//再插入到当前父级的下面
				trObj.after(tr);
				
			})
			$('.removeSpec').live('click',function(){
				$(this).parents('tr').remove();
			})

//	        	列表图
	            $('#f').uploadify({
	                'formData'     : {//POST数据
	                    '<?php echo session_name();?>' : '<?php echo session_id();?>',
	                },
	                'fileTypeExts' : '*.jpg;*.png',
	                'swf'      : '{{__PUBLIC__}}/uploadify/uploadify.swf',
	                'uploader' : '{{U('uploadList')}}',//指定服务器上传的方法
	                'buttonText':'选择文件',
	                'fileSizeLimit' : '2000KB',
	                'uploadLimit' : 1,//上传文件数
	                'width':65,
	                'height':25,
	                'successTimeout':10,//等待服务器响应时间
	                'removeTimeout' : 0.2,//成功显示时间
	                'onUploadSuccess' : function(file, data, response) {
	                    //把php返回的数据转为json
	                    data=$.parseJSON(data);
	                    //获得图片地址
	                    var imageUrl = data.url;
	                    var li="<li>";
	                    li += "<img height=80 src='"+imageUrl+"'/>";
	                    li += "<input type='hidden' name='pic' value='"+data.path+"'/><a href='javascript:;' id='self-closes'>X</a>";
	                    li += "</li>";
	                    $("#uploadList ul").prepend(li);
	                }
	            });
	
				//关闭图片
	            var i = 1;
	            $('#self-closes').live('click',function(){
	                $(this).parent('li').remove();
		                i++;
	                $('#f').uploadify('settings','uploadLimit',i);
	                
	            })
	            
//	           	商品图册
				 $('#d').uploadify({
		                'formData'     : {//POST数据
		                    '<?php echo session_name();?>' : '<?php echo session_id();?>',
		                },
		                'fileTypeExts' : '*.jpg;*.png',
		                'swf'      : '{{__PUBLIC__}}/uploadify/uploadify.swf',
		                'uploader' : '{{U('upload')}}',//指定服务器上传的方法
		                'buttonText':'选择文件',
		                'fileSizeLimit' : '2000KB',
		                'uploadLimit' : 4,//上传文件数
		                'width':65,
		                'height':25,
		                'successTimeout':10,//等待服务器响应时间
		                'removeTimeout' : 0.2,//成功显示时间
		                'onUploadSuccess' : function(file, data, response) {
		                    //把php返回的数据转为json
		                    data=$.parseJSON(data);
		                    //获得图片地址
		                    var imageUrl = data.url;
		                    var li="<li>";
		                    li += "<img height=100 src='"+imageUrl+"'/>";
		                    li += "<input type='hidden' name='detail_img[]' value='"+data.path+"'/><a href='javascript:;' id='self-close'>X</a>";
		                    li += "</li>";
		                    $("#uploadLists ul").prepend(li);
		                }
		            });
		
					//关闭图片
		            var j = 1;
		            $('#self-close').live('click',function(){
		                $(this).parent('li').remove();
					        j++;
		                $('#d').uploadify('settings','uploadLimit',j);
		            })
	        });
	    </script>
	</body>
</html>
